package kr.hkit.powerjava.chapter24.submit2.service;

import java.util.Scanner;

import kr.hkit.powerjava.chapter24.submit2.dao.InterfaceDao;


public abstract class ServiceItem<T> {

	protected InterfaceDao<T> dao; 
	protected Scanner sc;
	
	public ServiceItem(InterfaceDao<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
		prnMenu();
	}
	
	public abstract T inputItemInfo(boolean gubun);
	public abstract void prnSubMenu();
	

	protected void init() {
		dao.loadFile();
	
	}
	
	protected void close() {
		dao.saveFile();
	}
	
	public void prnMenu() { //메뉴
		int res = 0;
		T item = null;
		do {
			System.out.print("1.입력\n2.삭제\n3.수정\n4.검색\n5.리스트보기\n6.종료\n");
			res = sc.nextInt();
			switch (res) {
				case 1:
					// 정보입력
					item = inputItemInfo(true);
					dao.insertItem(item);
					break;	
				case 2:
					//리스트를 보여주고 삭제할 번호를 입력받음
					dao.prnItemList(null); //기본값 보여줌
					System.out.print("삭제할 정보를 입력하시오.");
					item = inputItemInfo(false);					
					dao.deleteItem(item); 
					break;
				case 3: //수정. 리스트를 보여주고 수정할 학생정보를 입력받음
					dao.prnItemList(null); //기본값 보여줌
					item = inputItemInfo(true);
					dao.updateItem(item);
					break;
				case 4: // 검색
					item = inputItemInfo(false);// false면 학번,성명만 받게
					try {
						item = dao.searchItem(item);
						System.out.println(item);
					}catch(ArrayIndexOutOfBoundsException e) {
						System.out.println("해당 정보 없음");
					}
					break;
				case 5:
					prnSubMenu();
					break;
				/*case 6:
				
					sc.close();
					System.exit(0); // 정상종료하도록. 종료하면서 저장됨 
					break;*/
				default:
					close(); //savefile
					res = 7;
			}
		} while (res <= 6);
	}
		
	
	
}
